package com.dhecp.project.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.project.system.domain.SysMailTableInfo;
import com.dhecp.project.system.mapper.SysMailTableInfoMapper;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import com.dhecp.project.system.service.SysMailTableInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @program: tsecp
 * @description: 新邮件系统 服务实现层
 * @author: hlf
 * @create: 2021-11-26 10:01
 */
@Service
public class SysMailTableInfoServiceImpl implements SysMailTableInfoService {

    @Autowired
    private SysMailTableInfoMapper sysMailTableInfoMapper;

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

    /**通过id查询邮件
     * @return SysMailTableInfo
     */
    @Override
    public SysMailTableInfo queryMailByID(String id) {
        return sysMailTableInfoMapper.findByMailID(id);
    }
    
    /**
	 * 查询未读通知数
	 * @param params 无必要参数
	 * @return
	 * @author DH-Xxj
	 */
	@Override
	public Integer querySystemMailWds(Map<String, Object> params) {
		return sysMailTableInfoMapper.selectWds(params);
	}
	
    /**
     *	 通过传入的条件查询邮件列表
     * @param params
     * @return List-SysMailTableInfo
     * @throws Exception
     */
    @Override
    public List<SysMailTableInfo> queryMailList(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));		//常规查询条件SQL语句
        if (params.get("field") == null || params.get("field") == ""){
            String orderBy = "sendTime desc";
            PageUtils.startPageOrder(orderBy);
        }else{
            PageUtils.startPage();
        }
        return sysMailTableInfoMapper.findMailList(params);
    }
    
    /**
     *	 查询邮件列表存储过程
     * @param params
     * @return List-SysMailTableInfo
     * @throws Exception
     */
    @Override
    public List<SysMailTableInfo> findSystemMailList(Map<String, Object> params) throws Exception {
        return sysMailTableInfoMapper.getSystemMailList(params);
    }
    
    /**
     *	 搜索邮件列表存储过程
     * @param params
     * @return List-SysMailTableInfo
     * @throws Exception
     */
    @Override
    public List<SysMailTableInfo> querySystemMailList(Map<String, Object> params) throws Exception {
        return sysMailTableInfoMapper.searchSystemMailList(params);
    }

    /**新增邮件
     * @return MessageModel
     * @throws UnsupportedEncodingException 
     */
    @Override
    public AjaxResult insertMail(String mailInfoItems) throws UnsupportedEncodingException {
        if (StringUtils.isEmpty(mailInfoItems)) {
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }
        List<SysMailTableInfo> sysMailTableInfoList = JSON.parseArray(mailInfoItems, SysMailTableInfo.class);
        if (sysMailTableInfoList == null || sysMailTableInfoList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
        for (SysMailTableInfo sysMailTableInfo : sysMailTableInfoList) {
        	sysMailTableInfo.setSendTime(new Date());
        	sysMailTableInfo.setMailText(java.net.URLDecoder.decode(sysMailTableInfo.getMailText(),"UTF-8"));
            sysMailTableInfoMapper.insert(sysMailTableInfo);
        }
        return AjaxResult.success("操作成功！");
    }

    /**删除邮件
     * @return MessageModel
     */
    @Override
    public AjaxResult deleteMail(String mailInfoItems) {
        if (StringUtils.isEmpty(mailInfoItems)) {
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }
        List<SysMailTableInfo> sysMailTableInfoList = JSON.parseArray(mailInfoItems, SysMailTableInfo.class);
        if (sysMailTableInfoList == null || sysMailTableInfoList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
        for (SysMailTableInfo sysMailTableInfo : sysMailTableInfoList) {
            sysMailTableInfoMapper.deleteByID(sysMailTableInfo);
        }
        return AjaxResult.success("操作成功！");
    }

    /**修改邮件
     * @return MessageModel
     * @throws UnsupportedEncodingException 
     */
    @Override
    public AjaxResult updateMail(Map<String, Object> params) throws UnsupportedEncodingException {
        List<SysMailTableInfo> mailContentList = JSON.parseArray(params.get("mailInfoItems").toString(), SysMailTableInfo.class);
        if (mailContentList == null || mailContentList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
        for (SysMailTableInfo sysMailTableInfo : mailContentList) {
        	if(sysMailTableInfo.getMailText() != null)sysMailTableInfo.setMailText(java.net.URLDecoder.decode(sysMailTableInfo.getMailText(),"UTF-8"));
            sysMailTableInfoMapper.updateMail(sysMailTableInfo);
        }
        return AjaxResult.success("操作成功！");
    }

}